package twitter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Monitor extends JFrame {
	private static final long serialVersionUID = 1L;

	Client client;

	JLabel speed;
	JLabel thread;
	JLabel totalUsers;
	JLabel totalTweets;
	JLabel totalTraffic;
	JLabel runningTime;
	JButton plus;
	JButton minus;
	JButton stop;
	JTextField server;
	JTextField port;
	JTextField idField;
	JLabel status;
	JTextArea info;
	static ButtonGroup typeOfIdGroup;
	
	public void setupMonitor(String serverAddress, String serverPort, String optionalUserId) {
		client = new Client(this);

		this.setSize(340, 150);
		this.setLayout(new GridLayout(11,1));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label;
		Container container;
		
		// Speed display
		container = new Container();
		container.setLayout(new FlowLayout());
		speed = new JLabel("0 tweets/sec");
		speed.setFont(new Font("Serif", Font.BOLD, 30));
		speed.setForeground(Color.red);
		container.add(speed);
		this.add(container);
		
		// Threads display
		container = new Container();
		container.setLayout(new FlowLayout());
		label = new JLabel("Max Threads:");
		thread = new JLabel(String.valueOf(client.maxPageFetcherThreads));
		thread.setForeground(Color.red);
		plus = new JButton("+");
		plus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	client.increaseMaxPageFetcherThreads();
            	thread.setText(client.getMaxPageFetcherThreads()+"");
            }
        });
		minus = new JButton("-");
		minus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	client.decreaseMaxPageFetcherThreads();
            	thread.setText(client.getMaxPageFetcherThreads()+"");
            }
        });
		container.add(label);
		container.add(thread);
		container.add(plus);
		container.add(minus);
		this.add(container);
		
		// Total users
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total users fetched:");
		totalUsers = new JLabel("0 user");
		totalUsers.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalUsers, BorderLayout.EAST);
		this.add(container);
		
		// Total tweets
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total tweets fetched:");
		totalTweets = new JLabel("0 tweet");
		totalTweets.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalTweets, BorderLayout.EAST);
		this.add(container);
		
		// Total traffic
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Total traffic");
		totalTraffic = new JLabel("0 bytes");
		totalTraffic.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(totalTraffic, BorderLayout.EAST);
		this.add(container);
		
		// Total running time
		container = new Container();
		container.setLayout(new BorderLayout());
		label = new JLabel("Running time:");
		runningTime = new JLabel("0 second");
		runningTime.setForeground(Color.red);
		container.add(label, BorderLayout.WEST);
		container.add(runningTime, BorderLayout.EAST);
		this.add(container);
		
		// Server address and port
		container = new Container();
		container.setLayout(new BorderLayout());
		server	= new JTextField(12);
		if(serverAddress != null) {
			server.setText(serverAddress);
		}
		else {
			server.setText("127.0.1");
		}
		port	= new JTextField(4);
		port.setAlignmentX(JTextField.CENTER_ALIGNMENT);
		if(serverPort != null) {
			port.setText(serverPort);
		}
		else {
			port.setText("5698");
		}
		container.add(server, BorderLayout.WEST);
		container.add(port, BorderLayout.EAST);
		this.add(container);
		
		// Update specific user
		container = new Container();
		container.setLayout(new BorderLayout());
		idField	= new JTextField(7);
		idField.setAlignmentX(JTextField.CENTER_ALIGNMENT);
		if(optionalUserId != null) {
			idField.setText(optionalUserId);
		}
		else {
			idField.setText("");
		}
		
		Container subContainer = new Container();
		subContainer.setLayout(new FlowLayout());
	    typeOfIdGroup = new ButtonGroup();
		JRadioButton radio;
		radio = new JRadioButton("Auto", true);
		subContainer.add(radio);typeOfIdGroup.add(radio);
		radio = new JRadioButton("Id", false);
		subContainer.add(radio);typeOfIdGroup.add(radio);
		radio = new JRadioButton("Name", false);
		subContainer.add(radio);typeOfIdGroup.add(radio);
		container.add(subContainer, BorderLayout.WEST);

		container.add(idField, BorderLayout.EAST);
		this.add(container);

		// State button
		stop = new JButton("Start");	
		stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	if(!client.isAlive()) {
            		setTextFieldsEnabled(false);
            		client.start();
            	}
            	else {
            		client.toggleState();
            	}
            }
        });
		this.add(stop);
		
		// Statusbar
		container = new Container();
		container.setLayout(new FlowLayout());
		status = new JLabel("idle");
		status.setEnabled(false);
		container.add(status);
		this.add(container);
		
		// Statusbar
		container = new Container();
		container.setLayout(new FlowLayout());
		info = new JTextArea("");
		info.setEnabled(false);
		info.setSize(340, 30);
		info.setLineWrap(true);
		container.add(info);
		this.add(container);

		// Make visible
		this.pack();
		this.setVisible(true);
	}
	
	public void setTextFieldsEnabled(boolean enabled) {
		server.setEnabled(enabled);
		port.setEnabled(enabled);
		idField.setEnabled(enabled);
	}
	
	public static JRadioButton getIdTypeSelection() {
	    for (Enumeration e = typeOfIdGroup.getElements(); e.hasMoreElements();) {
	      JRadioButton b = (JRadioButton) e.nextElement();
	      if (b.getModel() == typeOfIdGroup.getSelection()) {
	        return b;
	      }
	    }
	    return null;
	  }
}
